package com.zq.ht.web.service.content;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zq.ht.common.util.CommonUtil;
import com.zq.ht.common.util.FileRWUtil;
import com.zq.ht.common.util.PageUtil;
import com.zq.ht.dao.TProductMapper;
import com.zq.ht.model.TProduct;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by admin on 2016/6/26.
 */
@Service
public class ProductService {
    @Autowired
    private TProductMapper productMapper;

    public PageUtil getProductList(PageUtil pageUtil,int type){
        TProduct entity = new TProduct();
        if(type==1){

        }
        Page page = new Page(pageUtil.getRowStart()+1,pageUtil.getRowCount());
        List listAll = productMapper.selectList(RowBounds.DEFAULT,new EntityWrapper<TProduct>(entity,"id desc"));
        pageUtil.setTotalRows(listAll.size());
        List list = productMapper.selectList(page,new EntityWrapper<TProduct>(entity,"id desc"));
        pageUtil.setResults(list);
        return pageUtil;
    }

    public void saveProduct(TProduct product,MultipartHttpServletRequest request) throws IOException {
        //文件保存本地目录路径
        String savePath = request.getServletContext().getRealPath("/") + "attached/product/";
        File f = new File(savePath);
        if(!f.exists()){
            f.mkdirs();
        }
        //文件保存目录URL
        String saveUrl = request.getContextPath() + "/attached/product/";

        String picname = CommonUtil.getPicName();
        // 获得文件：
        MultipartFile file = request.getFile("pic");
        InputStream in = file.getInputStream();
        FileRWUtil.readBlob(in,savePath+picname);

        product.setPicUrl(saveUrl+picname);
        productMapper.insert(product);
    }

    public void updateProduct(TProduct product,MultipartHttpServletRequest request) throws IOException {
        //文件保存本地目录路径
        String savePath = request.getServletContext().getRealPath("/") + "attached/product/";
        File f = new File(savePath);
        if(!f.exists()){
            f.mkdirs();
        }
        //文件保存目录URL
        String saveUrl = request.getContextPath() + "/attached/product/";

        String picname = CommonUtil.getPicName();
        // 获得文件：
        MultipartFile file = request.getFile("pic");
        InputStream in = file.getInputStream();
        FileRWUtil.readBlob(in,savePath+picname);

        product.setPicUrl(saveUrl+picname);
        productMapper.insert(product);
    }

    public void deleteProduct(int id) {
        TProduct product = productMapper.selectById((long)id);
        deleteFile(product.getPicUrl());
        productMapper.deleteById((long)id);
    }

    public static void deleteFile(String sPath) {
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        System.out.print(file.isFile()+"-------------------");
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }

    public static void main(String[] agrs){
        ProductService.deleteFile("");
    }

}
